package com.hundsun.cprs.yyt.service.form.dto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.util.StringUtils;
import com.hundsun.cprs.yyt.service.form.domain.Form;
import com.hundsun.cprs.yyt.service.form.domain.FormAttrValues;
import com.hundsun.cprs.yyt.service.form.field.FieldWrapperUtil;
import com.hundsun.cprs.yyt.service.form.field.wrap.AbstractField;
import com.hundsun.cprs.yyt.util.BeanUtils;
import com.hundsun.cprs.yyt.service.form.domain.Form;
import com.hundsun.cprs.yyt.service.form.domain.FormAttrValues;
import com.hundsun.cprs.yyt.service.form.field.FieldWrapperUtil;
import com.hundsun.cprs.yyt.service.form.field.wrap.AbstractField;

public class FormInfo extends Form {

    private List<FormAttrInfo> formAttrInfoList;
    
    private Map<String,FormAttrInfo> formAttrInfoMap;
    
    private List<FormAttrValues> formAttrValues;
    
    private Map<String,String> formAttrValuesMap;
    
    private List<AbstractField> wrappedFields;
    
    private Long objectId;

    public List<FormAttrInfo> getFormAttrInfoList() {
        return formAttrInfoList;
    }

    public void setFormAttrInfoList(List<FormAttrInfo> formAttrInfoList) {
        this.formAttrInfoList = formAttrInfoList;
        if (formAttrInfoList != null) {
            this.formAttrInfoMap = BeanUtils.mapByKey("attrKey", formAttrInfoList);
        }
    }
    
    public Long getObjectId() {
        return objectId;
    }

    public void setObjectId(Long objectId) {
        this.objectId = objectId;
    }
    
    public boolean containKey(String attrKey){
        if (formAttrInfoMap == null) {
            return false;
        }
        return formAttrInfoMap.containsKey(attrKey);
    }
    
    public String getAttrValue(String name){
        if (this.formAttrValuesMap == null || this.formAttrValuesMap.size() == 0) {
            return null;
        }
        return this.formAttrValuesMap.get(name);
    }
    
    public AbstractField getWrappedField(String attrKey){
        if (wrappedFields == null || wrappedFields.size() == 0) {
            return null;
        }
        AbstractField matchField = null;
        for(AbstractField item : wrappedFields){
            if (attrKey.equals(item.getAttrKey())) {
                matchField = item;
            }
        }
        return matchField;
    }
    
    /**
     *  包装属性，渲染
     */
    public void render(){
        if (wrappedFields == null && formAttrInfoList != null) {
            wrappedFields = new ArrayList<AbstractField>();
            for(FormAttrInfo source : this.formAttrInfoList){
                wrappedFields.add(FieldWrapperUtil.wrapFormAttr(source,this));
            }
        }
    }
    
    public List<FormAttrValues> loadValues(Map<String,String> valuesMap){
        if (formAttrInfoList == null || formAttrInfoList.size() == 0) {
            return null;
        }
        List<FormAttrValues> formAttrValuesList = new ArrayList<FormAttrValues>();
        for(FormAttrInfo formAttr : formAttrInfoList){
            FormAttrValues formAttrValue = new FormAttrValues();
            formAttrValue.setFormId(getId());
            formAttrValue.setObjectId(getObjectId());
            formAttrValue.setAttrId(formAttr.getId());
            formAttrValue.setAttrKey(formAttr.getAttrKey());
            formAttrValue.setAttrValue(valuesMap.get(formAttr.getAttrKey()));
            formAttrValuesList.add(formAttrValue);
            
            //回填属性DTO中的属性值
            formAttr.setAttrValue(formAttrValue.getAttrValue());
        }
        initAttrValues(formAttrValuesList);
        return formAttrValuesList;
    }
    
    public List<FormAttrValues> loadValues(List<FormAttrValues> rawDbFormAttrValues){
        if (formAttrInfoList == null || formAttrInfoList.size() == 0) {
            return null;
        }
        List<FormAttrValues> formAttrValuesList = new ArrayList<FormAttrValues>();
        for(FormAttrInfo formAttr : formAttrInfoList){
            for(FormAttrValues formAttrValue : rawDbFormAttrValues){
                if (StringUtils.equals(formAttr.getAttrKey(), formAttrValue.getAttrKey())) {
                    formAttrValuesList.add(formAttrValue);
                    //回填属性DTO中的属性值
                    formAttr.setAttrValue(formAttrValue.getAttrValue());
                }
            }
        }
        initAttrValues(formAttrValuesList);
        return formAttrValuesList;
    }
    
    private void initAttrValues(List<FormAttrValues> formAttrValuesList){
        if (formAttrValuesList == null || formAttrValuesList.size() == 0) {
            return;
        }
        this.formAttrValues = formAttrValuesList;
        Map<String,String> attrValuesMap = new HashMap<String,String>();
        for(FormAttrValues item : formAttrValuesList){
            attrValuesMap.put(item.getAttrKey(), item.getAttrValue());
        }
        this.formAttrValuesMap = attrValuesMap;
    }

    public List<AbstractField> getWrappedFields() {
        return wrappedFields;
    }

}
